<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2021-12-01 09:56:59
 * @LastEditors: Sam
 * @LastEditTime: 2022-12-30 10:07:29
 */
namespace app\api\controller;
use app\__MODULE__\model\{Question as QuestionModel,Answer,Option,User};
use think\facade\Db;
use think\Request;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("答题")
 */
class Question extends ApiBase{




    /**
     * @Apidoc\Title("获取题目")
     * @Apidoc\Url("/api/get_question")
     * @Apidoc\Desc("需要token验证")
     * @Apidoc\Method("get")
     * @Apidoc\Returned("id", type="int",desc="问题id")
     * @Apidoc\Returned("title", type="string", desc="题目标题")
     * @Apidoc\Returned("pic", type="string", desc="题目图片")
     * @Apidoc\Returned ("option",type="[]",desc="选项答案",
     *          @Apidoc\Returned ("option_id",type="int",desc="答案id"),
     *          @Apidoc\Returned ("option_name",type="string",desc="答案"),
     *          @Apidoc\Returned ("option_number",type="string",desc="答案编号")
     *     )
     */
    public function get_question(Request $request){
        $uid = $this->get_user_id();
        $question_id  = QuestionModel::get_question($uid);
        if(!$question_id) json_response(0,'取题异常，请重试');
        $question = QuestionModel::where('id',$question_id)->append(['option'])->field('id,title,pic')->find()->toArray();
        json_response(1,'success',$question);
    }
     
    /**
     * @Apidoc\Title("提交答案")
     * @Apidoc\Url("/api/submit_question")
     * @Apidoc\Desc("需要token验证")
     * @Apidoc\Method("post")
     * @Apidoc\Param("question_id", type="int",require=true,desc="问题id")
     * @Apidoc\Param("option_id",   type="int",require=true, desc="答案id")
     * @Apidoc\Returned("is_right", type="int", desc="是否正确0错误 1正常")
     * @Apidoc\Returned ("answer_desc",type="object",desc="答案解析",
     *          @Apidoc\Returned ("title",type="string",desc="题目"),
     *          @Apidoc\Returned ("remark",type="string",desc="解析"),
     *          @Apidoc\Returned ("answer_number",type="string",desc="答案编号")
     *     )
     */
    public function submit_question(){
        $uid = $this->get_user_id();
        $question_id = param_check('question_id');;
        $option_id   = param_check('option_id');
        redis_lock("{$this->redis_key}:submit_question:{$uid}", $uid, function ($params) use ($uid,$question_id,$option_id) {
            Db::startTrans();
            try{
                if(!$question    = QuestionModel::find($question_id)) exception('题目不存在');
                if(!$option      = Option::find($option_id)) exception('选项不存在');
                if(Answer::where('uid',$uid)->where('question_id',$question_id)->count()) exception('该题已回答过');
                //其他限制检查 如答题次数限制 答题时间限制 start
    
                //其他显示检查 end
                $answer_option = explode(',',$option_id); //回答的答案数组
                $question_option = explode(',',$question->answer_id); //题目的答案数组
                //交叉对比
                $array_intersect  = array_intersect($answer_option,$question_option); //交集
                if(count($array_intersect) == count($question_option) && count($answer_option) == count($question_option)){
                    //全对
                    //回答正确
                    $answer =  Answer::create([
                        'uid'          => $uid,
                        'question_id'  => $question->id,
                        'answer_number'=> $option->option_number,
                        'answer_id'    => $option->id,
                        'is_right'     => 1
                    ]);
                   //答题正确后其他后续处理  如增加用户积分，经验值  积分日志写入 start
    
                   //其他后续处理 end
                   //返回数据
                   
                   $return_data = [
                         'is_right' => 1
                    ];
                }elseif(count($array_intersect) && count($answer_option) < count($question_option)){
                    //部分答对相应处理
                    $answer =  Answer::create([
                        'uid'          => $uid,
                        'question_id'  => $question->id,
                        'answer_number'=> $option->option_number,
                        'answer_id'    => $option->id,
                        'is_right'     => 3
                    ]);
                    $return_data = [
                        'is_right' => 3
                   ];
                }else{
                    //错误
                     //回答错误
                     $answer =  Answer::create([
                        'uid'          => $uid,
                        'question_id'  => $question->id,
                        'answer_number'=> $option->option_number,
                        'answer_id'    => $option->id,
                        'is_right'     => 0
                    ]);
                     //答题错误后其他后续处理 start
                    //其他后续处理 end
    
                     //返回数据
                   $return_data = [
                        'is_right' => 0,
                        'answer_desc'=>[ //错误题目
                            'title' => $question->title, //标题
                            'answer_number' => $question->right_answer, //正确答案
                            'remark'   => $question->remark //解析
                        ],
                    ];
                }
                Db::commit();
                return success($return_data);
            }catch(\Exception $e){
                Db::rollback();
                return error($e->getMessage());
            }
        },3);
    }
}